package leetbook.sort_search;

/**
 * 山脉数组的峰顶索引
 * 思路：寻找最小的满足arr[i]>arr[i+1]的i
 */
public class Solution852 {

    public int peakIndexInMountainArray(int[] arr) {
        int l = 1, r = arr.length - 2, mid = 0;
        while (l<=r){
            mid = (l+r)>>1;
            if(arr[mid]<arr[mid+1]){ //mid不满足条件
                l = mid+1;
            }else if(arr[mid]<arr[mid-1]){ //mid满足条件但不是最小的i
                r = mid-1;
            }else{ //mid满足条件且最小
                break;
            }
        }
        return mid;
    }
}
